package com.foo.ui_demo_widget1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        //数据（Data）<-->适配器（Adapter）<-->界面（Spinner）
        Spinner spSemester = findViewById(R.id.sp_semester);
        //默认选择第一个选项

        //region 学期下拉框：Spinner加载静态数据
        spSemester.setSelection(0, true);
        //监听器
        spSemester.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int postition, long id) {
                if (postition != 0) {
                    //提示信息
                    //（目标活动，获得选项信息，简短展示）.show 展示
                    Toast.makeText(SpinnerActivity.this,
                            parent.getItemAtPosition(postition).toString(),
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });//endregion
    }
}